var goodType={"1":"护理服务","2":"普通服务","3":"床旁点餐","4":"普通商品"}
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'web/basegoods/list',
        datatype: "json",
        colModel: [			
			{ label: '初始数据ID', name: 'id_', width: 11, key: true,hidden:true},
			{ label: 'id', name: 'id', width: 45, key: true,hidden:true},
			{ label: '商品总栏目类别id', name: 'goodsTypeId',hidden:true},
			{ label: '商品类别', name: 'goodsTypeName', width: 45,formatter:function(cellvalue, options, rowObject){
				if(!rowObject.goodsTypeId) {
					return  '';
				}
				return  goodType[rowObject.goodsTypeId];
        	}},
			{ label: '商品编码', name: 'NO', width: 45},
            { label: '商品名称', name: 'name', width: 45},
			{ label: '商品关键字', name: 'keyName', width: 45 },
			{ label: '商品简介', name: 'introState', width: 45,hidden:true},
			{ label: '商品详情', name: 'introDetail', width: 45,hidden:true},
			{ label: '购买须知', name: 'introNotice', width: 45,hidden:true},
			{ label: '商品库存', name: 'stockNum', width: 45},
			{ label: '商品单价', name: 'price', width: 45},
			{ label: '商家id', name: 'shopsId', hidden:true},
			{ label: '商家', name: 'shopsName', width: 45},
			{ label: '发布时间', name: 'publishTime', width: 55},
			{ label: '下架时间', name: 'downTime', width: 45},
			{ label: '是否下架', name: 'isDown', width: 45,formatter:function(value, options, rowObject){
				if(value === 0) {
					ret = '<span class="label label-success">上架</span>';
				} else if(value === 1) {
					ret = '<span class="label label-danger">下架</span>';
				}
				return ret;
        	}}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    
    new AjaxUpload('#upload', {
        action: baseURL + 'sys/oss/upload?token=' + token,
        name: 'file',
        autoSubmit:true,
        responseType:"json",
        onSubmit:function(file, extension){
            if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))){
                alert('只支持jpg、png、gif格式的图片！');
                return false;
            }
        },
        onComplete : function(file, r){
            if(r.code == 0){
                vm.goods.url=r.url;
            }else{
                alert(r.msg);
            }
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			name: null
		},
		showList: true,
		title:null,
		goods:{ 
		},
	    shopsList:[{'value':1,'text':'天下布医'}]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.goods = {goodsTypeId:"4"};
			umState.setContent('');
			umDetail.setContent('');
			ueNotice.setContent('');
			umState.reset();
			umDetail.reset();
			ueNotice.reset();
		},
		update: function () {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
			vm.getInfo(id);

		},
		del: function () {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "web/basegoods/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
                                vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function () {
			var url = vm.goods.id_ == null ? "web/basegoods/save" : "web/basegoods/update";
			if(!vm.goods.url) {
				alert("请上传图片");
				return;
			} 
			
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.goods),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getInfo: function(proId){
			$.get(baseURL + "web/basegoods/info/"+proId, function(r){
				vm.goods = r.goods;
				if(r.goods.introState) {
					umState.setContent(vm.goods.introState);
				} else {
                	umState.setContent("");
                }
				
				if(r.goods.introDetail) {
					umDetail.setContent(vm.goods.introDetail);
				} else {
					umDetail.setContent("");
				}
				
				if(r.goods.introNotice) {
					ueNotice.setContent(vm.goods.introNotice);
				} else {
					ueNotice.setContent("");
				}
				umState.reset();
				umDetail.reset();
				ueNotice.reset();
			});
		},
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'name': vm.q.name},
                page:page
            }).trigger("reloadGrid");
		}
	}
});